package com.freight.user.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.redis.RedisTemplateHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import sun.security.provider.MD5;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class GpsUtil {

    private String username="dcwl";
    private String password="123456";
    private static final String anUrl="https://www.gps51.com/webapi?";

    public static void main(String[] args) {
        JSONObject data=new JSONObject();
        List list = new ArrayList();
//        list.add("53810829004");
//        list.add("53810791856");
//        data.put("deviceids",list);
//        data.put("lastquerypositiontime",0);
//        data.put("username","minigps");
//        data.put("password","e10adc3949ba59abbe56e057f20f883e");
//        data.put("from","web");
//        data.put("type","USER");

        data.put("deviceid","53810829004");
        data.put("begintime","2022-11-12 08:00:00");
        data.put("endtime","2022-11-13 08:00:00");
        data.put("timezone",8);

//        HttpPost httpPost = new HttpPost("https://www.gps51.com/webapi?action=login");
//        HttpPost httpPost = new HttpPost("https://www.gps51.com/webapi?action=lastposition&token=418bcdbea16cecf5fa29b7f88d51970e");
          HttpPost httpPost = new HttpPost("https://www.gps51.com/webapi?action=querytracks&token=418bcdbea16cecf5fa29b7f88d51970e");

        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        StringEntity entity = new StringEntity(JSON.toJSONString(data), ContentType.create("application/json", "UTF-8"));
        httpPost.setEntity(entity);
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

        CloseableHttpResponse closeableHttpResponse;

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        String content_synchronization="";
        try {
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {

//                closeableHttpResponse.getAllHeaders();
//                closeableHttpResponse.getLocale();
//                closeableHttpResponse.getProtocolVersion();
//
//                HttpParams httpParams = closeableHttpResponse.getParams();

                HttpEntity httpEntity = closeableHttpResponse.getEntity();
                content_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
                JSONObject json = JSONObject.parseObject(content_synchronization);
                System.out.println(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void refreshToken(RedisTemplateHelper redisTemplate){

        JSONObject data=new JSONObject();
        data.put("username",username);
        data.put("password",DigestUtils.md5Hex(password));
        data.put("from","web");
        data.put("type","USER");

        String url = anUrl + "action=login";
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        StringEntity entity = new StringEntity(JSON.toJSONString(data), ContentType.create("application/json", "UTF-8"));
        httpPost.setEntity(entity);

        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse;

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        String content_synchronization="";
        JSONObject json = new JSONObject();
        try {
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity httpEntity = closeableHttpResponse.getEntity();
                content_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
                json = JSONObject.parseObject(content_synchronization);
                System.out.println(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAA"+json);
        if(json.getInteger("status")==0){
            System.out.println(json);
            redisTemplate.set("gpsRefreshToken",json.getString("token"),23, TimeUnit.HOURS);
        }else {
            throw new FreightException("获取GPS位置出错");
        }

    }

    public JSONObject gpsPost(JSONObject data,String requestType,RedisTemplateHelper redisTemplate){
        if(redisTemplate.get("gpsRefreshToken")==null){
            refreshToken(redisTemplate);
        }
        String url = anUrl + requestType + "token=" + redisTemplate.get("gpsRefreshToken");
        System.out.println(url);
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        StringEntity entity = new StringEntity(JSON.toJSONString(data), ContentType.create("application/json", "UTF-8"));
        httpPost.setEntity(entity);

        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse;

        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        String content_synchronization="";
        JSONObject json = new JSONObject();
        try {
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity httpEntity = closeableHttpResponse.getEntity();
                content_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
                json = JSONObject.parseObject(content_synchronization);
                System.out.println(json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  json;
    }

}
